<template>
<view class="page">
	<view class="header-box">
		<view class="icon-nav" @click="goBack"></view>
		<view class="search-box">
			<picker class="goods-item" @change="typeBtn" range-key="name" :range="typeArr">
				{{type_label}} <text class="icon-tri"></text>
			</picker>
			<view class="line"></view>
			<input type="text" value="" :placeholder="placeholder" readonly @click="onSearch" placeholder-class="search-placeholder" class="search-input" />
		</view>
	</view>
	<view class="cate-page">
		<scroll-view scroll-y="true" class="cate-box">
			<view class="cate-item-box" v-for="(a,i) in selectNav" >
				<view class="cate-item" :class="{on:active === a.category_id}" @click="selectBtn(a)">
					{{a.name}}
				</view>
			</view>
		</scroll-view>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="lower">
			<view class="cate-goods" v-if="!empty">
				<navigator class="goods-item" v-for="i in list" :url="'/mallShop/home/goodsDtl?id=' + i.goods_id" hover-class="none" >
					<image :src="i.goods_image" class="goods-img" mode=""></image>
					<view class="goods-title">{{ i.goods_name }}</view>
				</navigator>
			</view>
			
			<view class="empty" v-else>
				<img src="/mall/static/images/no-goods.png" alt="">
				暂无相关商品
			</view>
		</scroll-view>
	</view>
	
	
	<MallFoot :on="1"></MallFoot>
</view>
</template>

<script>
	import {tabs, shopList} from '@/api/substitution.js'
		
	import MallFoot from '@/components/mallFoot/mallFoot'
	export default {
		components:{
			MallFoot,
		},
		data() {
			return {
				typeArr:[{name:'商品'},{name:'商户'}],
				type_label:'商品',
				
				selectNav:[],
				active: null,
				
				page: 1,
				
				list: [],
				name: '',
				
				scrollTop: 0,
				
				is_load: true,  // true为第一页时，false为分页
				
				total: null,  // 总页数
				
				empty: false,
				
				placeholder: '请输入商品...'
			}
		},
		onLoad () {
			this.onTabs()
			
			this.onTableList()
		},
		
		// 下拉刷新
		onPullDownRefresh() {
				uni.stopPullDownRefresh()
		},
		
		methods: {
			typeBtn(e){
				console.log(e)
				this.type_label = this.typeArr[e.detail.value].name
			},
			
			// 返回上一页
			goBack() {
				uni.navigateBack()
			},
			
			selectBtn(e){
				this.active = e.category_id
				this.is_load = true
				this.name = e.name
				this.page = 1;
				this.onTableList();
			},
			
			// 跳转搜索页
			onSearch () {
				uni.navigateTo({
					url: '/mallShop/home/search?url=2&type_label=' + this.type_label
				})
			},
			
			// 上拉加载
			lower (e) {
				if (this.page < this.total) {
					this.page += 1;
					this.is_load = false
					this.onTableList()
				}
			},
			
			// 获取商品分类
			onTabs () {
				tabs('').then(res => {
					this.selectNav = res.data.list;
					this.active = res.data.list[0].category_id
					this.name = res.data.list[0].name
				})
			},		
			
			// 获取商品列表
			onTableList() {
				shopList({
					sortType: 'all',
					page: this.page,
					sortPrice: 0,
					category_id: this.active,
					goods_type: 10,
					search: ''
				}).then(res => {
					// 判断第一页数据是否为空
					if (this.page === 1 && res.data.list.data.length === 0) {
						this.empty = true
						this.list = [];
					} else {
						this.empty = false
						res.data.list.data.forEach(item => {
							item.amount = item.goods_sku.line_price.toString().split('.')[0];
							item.zero = item.goods_sku.line_price.toString().split('.')[1];
						})
						this.total = Math.ceil(res.data.list.total / res.data.list.per_page)
						if (this.is_load) {
							this.list = res.data.list.data;
						} else {
							res.data.list.data.forEach(item => {
								this.list.push(item)
							})
						}
					}
				})
			},
		},
	}
</script>

<style scoped lang="scss">
	@import '/common/css/shop.css';
	@mixin heightCenter ($num){
		height: $num+rpx;
		line-height: $num+rpx;
	}
	@mixin flexCenter(){
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.header-box{
		background: linear-gradient(180deg, #FE4A32 0%, #FE7E48 100%);
		width: 750rpx;
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		padding: 60rpx 30rpx 30rpx;
		.icon-nav{
			margin-right: 40rpx;
			position: relative;
			top:10rpx;
		}
		.search-box{
			background-color: #FFFFFF;
			height: 70rpx;
			border-radius: 35rpx;
			width: 590rpx;
			display: flex;
			align-items: center;
			.goods-item{
				width: 120rpx;
				position: relative;
				top: -2rpx;
				text-align: center;
				color: #888;
			}
			.line{
				width: 2rpx;
				height: 40rpx;
				background-color: #EFEFEF;
			}
			.search-input{
				flex:1;
				@include heightCenter(70);
				color: #333333;
				padding-left:60rpx;
				background: url('/static/images/mallShop/search.png') no-repeat 15rpx center;
				background-size: 30rpx 30rpx;
			}
			.search-placeholder{
				color: #bbb;
			}
		}
	}
	.cate-page{
		display: flex;
		padding-top: 164rpx;
		height: calc(100vh - 164rpx - 100rpx);
		overflow: hidden;
		.cate-box,
		.cate-goods{
			overflow-y: scroll;
		}
		.cate-box{
			width: 230rpx;
			background-color: #FFFFFF;
			.cate-item-box{
				height: 120rpx;
				@include flexCenter();
				.cate-item{
					width: 200rpx;
					height: 80rpx;
					font-size: 28rpx;
					@include  flexCenter();
					border-radius: 120rpx;
					&.on{
						background-color: #FFF5F1;
						color: rgba(254, 124, 71, 1);
						font-size: 32rpx;
					}
				}
			}
		}
		.scroll-Y {
			flex:1;
		}
		.cate-goods{
			padding:50rpx;
			background-color: #F8F8F8;
			padding-bottom:20rpx;
			&::after{
				content: " ";
				display: block;
				clear: both;
			}
			.goods-item{
				width: 120rpx;
				margin-bottom:20rpx;
				height: 200rpx;
				float:left;
				&:nth-child(3n+2){
					margin-left:30rpx;
					margin-right:30rpx;
				}
				.goods-img{
					width: 120rpx;
					height: 120rpx;
					border-radius: 10rpx;
				}
				.goods-title{
					display: -webkit-box;
					-webkit-line-clamp:2;
					-webkit-box-orient:vertical;
					text-overflow: ellipsis;
					line-height: 1.4;
					overflow: hidden;
					font-size: 25rpx;
					color: #333333;
				}
			}
		}
	}

.empty {
	height: 100%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	font-size: 30rpx;
	color: #333;
			
	img {
		width: 300rpx;
		margin-bottom: 30rpx;
	}
}
</style>
